<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <!-- 数据列表 -->
    <div class="flex mt10">
      <el-card shadow="never" :body-style="{ padding: '10px 20px 10px' }" style="margin-right: 20px; flex: 1">
        <div slot="header" class="mb9 f16 flex btns">
          <span style="font-weight: 600">五金库存查询</span>
        </div>
        <Table :tableData="tableData" :tableHeader="tableHeader" @currentRow="currentRow" />

        <!-- 分页器 -->
        <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
      </el-card>

      <el-card shadow="never" :body-style="{ padding: '0' }">
        <el-card shadow="never" :body-style="{ padding: '10px 20px 0px' }">
          <div slot="header" class="mb9 f16 flex">
            <span style="font-weight: 600">入库明细查询</span>
          </div>
          <Table :tableData="tableData2" :tableHeader="tableHeader1" :TableHeight="50" :outTableHeight="75" />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '10px 20px 10px' }">
          <div slot="header" class="mb9 f16 flex">
            <span style="font-weight: 600">出库明细查询</span>
          </div>
          <Table :tableData="tableData3" :tableHeader="tableHeader2" :TableHeight="50" :outTableHeight="75" />
        </el-card>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'DepletingInventory'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import fileRequest from '@/utils/fileRequest'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入物料编码',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '收货日期',
    dateValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'select',
    selectP: '分类',
    selectValue: '',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
])
const tableHeader = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '入库日期',
    prop: 'inDate'
  },
  {
    label: '发料日期',
    prop: 'outDate'
  },
  {
    label: '入库数量',
    prop: 'inQty'
  },
  {
    label: '发料数量',
    prop: 'outQty'
  },
  {
    label: '库存数量',
    prop: 'stockQty'
  }
])

const tableHeader1 = ref([
  {
    label: '日期',
    prop: 'slipDate'
  },
  {
    label: '单据号码',
    prop: 'slipNo'
  },
  {
    label: '单据类型',
    prop: 'slipReason'
  },
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '数量',
    prop: 'qty'
  }
])

const tableHeader2 = ref([
  {
    label: '日期',
    prop: 'slipDate'
  },
  {
    label: '单据号码',
    prop: 'slipNo'
  },
  {
    label: '单据类型',
    prop: 'slipReason'
  },
  {
    label: '部门',
    prop: 'deptName'
  },
  {
    label: '数量',
    prop: 'qty'
  }
])

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    // if (!formList.value[2].dateValue) {
    //   JMessage.warning('请选择入库日期')
    //   return
    // }
    const res = (await fileRequest.post(`/pcOrder/downloadStockPart`, {
      inEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[1],
      inStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
      )?.dateValue?.[0],
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
      )?.inputValue,
      mtlNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
      )?.inputValue,
      outEndDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
      )?.dateValue?.[1],
      outStartDate: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
      )?.dateValue?.[0],
      typeNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
        ?.selectValue?.split('  ')[0],
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const tableData2 = ref([])
const tableData3 = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcOrder/getStockPcSlipPart', {
    inEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[1],
    inStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '入库日期'
    )?.dateValue?.[0],
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue,
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入物料编码'
    )?.inputValue,
    outEndDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
    )?.dateValue?.[1],
    outStartDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '收货日期'
    )?.dateValue?.[0],
    typeNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类')
      ?.selectValue?.split('  ')[0],
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  let { mtlNo }: any = row
  // 给表二传指令号
  let table2Data = await service.post<any, any>(`/pcOrder/getStockOutAndIn?mtlNo=${mtlNo}`)
  tableData2.value = table2Data.inList
  tableData3.value = table2Data.outList
  // // 给表三传指令号

  // const targetItem = formList2.value.find((item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令');
  // if (targetItem) {
  //   targetItem.inputValue = lotNo;
  // }
}
const getSelectList = async () => {
  let category = formList.value.find(
    (item: any) => item.type === 'select' && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '分类'
  ) as any
  category.selectOptions?.push(
    {
      optionKey: '办公/生产用品类',
      optionValue: `p1  办公/生产用品类`
    },
    {
      optionKey: '机台/固定资产类',
      optionValue: `p2  机台/固定资产类`
    },
    {
      optionKey: '机修/零配件类',
      optionValue: `p3  机修/零配件类`
    },
    {
      optionKey: '胶药水类',
      optionValue: `p4  胶药水类`
    },
    {
      optionKey: '楦头类',
      optionValue: `p5  楦头类`
    },
    {
      optionKey: '网板类',
      optionValue: `p6  网板类`
    },
    {
      optionKey: '模具类',
      optionValue: `p7  模具类`
    },
    {
      optionKey: '斩刀类',
      optionValue: `p8  斩刀类`
    }
  )
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-child(5)) {
  .el-select {
    width: 200px;
    height: 28px;
  }
}

// 主体样式
.ProductionDynamicMain {
  width: 100%;
  margin-top: 20px;

  .ProductionDynamicMainCard {
    width: 100%;
    box-shadow: none;
  }
}

.ProductionDynamicMainCardMain {
  width: 98%;
  height: 600px;
  display: flex;
  margin: auto;

  .ProductionDynamicMainCardMainLeft {
    width: 60%;
    height: 100%;
  }

  .ProductionDynamicMainCardMainRight {
    width: 20%;
    height: 100%;
  }

  .ProductionDynamicMainCardMainRightT {
    width: 20%;
    height: 100%;
    margin-left: 20rem;
  }
}

.ProductionDynamicMainCardBottom {
  margin: auto;
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}

.MainLeftCardH1 {
  font-size: 16px;
  margin: 10px 0 10px 0;
}

.ProductionDynamicMainCardBottomBtn {
  margin-top: 30px;
}

.ProductionDynamicMainCardMainRightTDivOne {
  width: 100%;
  height: 45%;
}

.ProductionDynamicMainCardMainRightTDivTwo {
  margin-top: 18px;
  width: 100%;
  height: 45%;
}

.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 40px;

  .total {
    font-size: 14px;
    color: rgba(0, 0, 0, 0.6);
    font-family: PingFangSC-Regular-, PingFangSC-Regular;
  }

  :deep(.el-input__wrapper) {
    padding: 0;
  }

  :deep(.el-input__suffix) {
    transform: translateX(-9px);
  }

  :deep(.el-pagination .el-select .el-input) {
    width: 88px;
  }

  :deep(.number) {
    border: 1px solid #dcdcdc;
    margin-right: 8px;
    border-radius: 3px 3px 3px 3px;
  }

  :deep(.is-active.number) {
    background-color: #f89027 !important;
    color: white;
    border-radius: 3px 3px 3px 3px;
  }

  .save {
    color: black;
    border: 1px solid #e7e7e7;
    background: #e7e7e7;
  }
}
</style>
